
import com.zzyl.RuoYiApplication;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@SpringBootTest( classes = RuoYiApplication.class)
public class test01 {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Test
    public void test011()
    {
        redisTemplate.opsForValue().set("test", "test");
        System.out.println(redisTemplate.opsForValue().get("test"));
    }
    /**
     * 操作字符串类型的数据
     */
    @Test
    public void testString() {
        // 普通的set方法
        redisTemplate.opsForValue().set("name", "张三");
        System.out.println(redisTemplate.opsForValue().get("name"));
        // 设置带有过期时间的key
        redisTemplate.opsForValue().set("token", "123qweasd",20, TimeUnit.SECONDS);
        System.out.println(redisTemplate.opsForValue().get("token"));

        // setnx 当指定的键key不存在时,会将key的值设置为value，返回true，否则返回false(不能覆盖原有值)
        System.out.println(redisTemplate.opsForValue().setIfAbsent("lock", "09876", 5, TimeUnit.MINUTES));
        System.out.println(redisTemplate.opsForValue().setIfAbsent("lock", "34567", 5, TimeUnit.MINUTES));
    }
    /**
     * 操作hash类型的数据
     */
    @Test
    public void testHash() {
        // hash请求   大key 小key  value
        redisTemplate.opsForHash().put("user", "name", "张三");
        redisTemplate.opsForHash().put("user", "age", "30");
        // 根据大key和小key获取值
        System.out.println(redisTemplate.opsForHash().get("user", "name"));

        // 根据大key获取所有的小key
        Set<Object> keys = redisTemplate.opsForHash().keys("user");
        System.out.println(keys);

        // 根据大key获取所有的值
        List<Object> values = redisTemplate.opsForHash().values("user");
        System.out.println(values);

        // 删除小key和值
        redisTemplate.opsForHash().delete("user", "age");

    }
}
